package com.zhengshang.common.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.zhengshang.common.constant.SmsConstant;

import java.util.Arrays;
import java.util.List;

/**
 * @author HY
 * @version 1.0.0
 * @ClassName Sample.java
 * @Description TODO
 * @createTime 2021年03月18日 15:56
 */
public class Sample {

    // 依赖的模块可通过下载工程中的模块依赖文件或右上角的获取 SDK 依赖信息查看
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = SmsConstant.SMS_URL;
        return new Client(config);
    }

    public static void sendSms(String phone, String code) throws Exception {
        JSONObject param = new JSONObject();
        param.put("code", code);
        Client client = Sample.createClient(SmsConstant.SMS_ACCESS_KEY_ID, SmsConstant.SMS_ACCESS_KEY_SECRET);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(SmsConstant.SMS_SIGN_NAME)
                .setTemplateParam(param.toJSONString())
                .setTemplateCode(SmsConstant.SMS_MODEL_CODE);
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
    }

}
